﻿using MySimpleMvc.Handler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace MySimpleMvc
{
    public class Global : HttpApplication
    {
        // 定义路由规则
        private static IList<string> Routes;

        protected void Application_Start(object sender, EventArgs e)
        {
            Routes = new List<string>();

            Routes.Add("{controller}/{action}");
            Routes.Add("{controller}");
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            //#region 方式一:伪静态方式实现路由映射服务
            ////获取当前请求的URL地址
            //var executePath = Request.AppRelativeCurrentExecutionFilePath;
            ////获取当前请求的参数数组
            //var paraArray = executePath.Substring(2).Split('/');
            ////如果么有参数则执行默认配置
            //if (string.IsNullOrEmpty(executePath) || paraArray.Length == 0)
            //{
            //    return;
            //}

            //string controllerName = "home";
            //if (paraArray.Length > 0 && !string.IsNullOrEmpty(paraArray[0]))
            //{
            //    controllerName = paraArray[0];
            //}

            //string actionName = "index";
            //if (paraArray.Length > 1 && !string.IsNullOrEmpty(paraArray[1]))
            //{
            //    actionName = paraArray[1];
            //}

            //Context.RewritePath(string.Format("~/Index.ashx?controller={0}&action={1}", controllerName, actionName));
            //#endregion

            #region 方式二:模拟路由表实现映射服务
            // 模拟路由字典
            IDictionary<string, string> routeData = new Dictionary<string, string>();
            foreach (var item in Routes)
            {
                // 获得当前请求的参数数组
                var executePath = Request.AppRelativeCurrentExecutionFilePath;

                // 如果没有参数则执行默认配置
                if (string.IsNullOrEmpty(executePath) || executePath.Equals("~/"))
                {
                    executePath += "home/index";
                }

                var executePathArray = executePath.Substring(2).Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                var routeKeys = item.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                if (executePathArray.Length == routeKeys.Length)
                {
                    for (int i = 0; i < routeKeys.Length; i++)
                    {
                        routeData.Add(routeKeys[i], executePathArray[i]);
                    }
                    // 入口一：单一入口 Index.ashx
                    //Context.RewritePath(string.Format("~/Index.ashx?controller={0}&action={1}", routeData["{controller}"], routeData["{action}"]));
                    // 入口二：指定MvcHandler进行后续处理
                    Context.RemapHandler(new MvcHandler(routeData));
                    // 只要满足一条规则就跳出循环匹配
                    
                    break;
                }

            }
            #endregion

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}